package com.jhws.business.car.service;

import com.jhws.business.car.bo.CarConstant;
import com.jhws.business.car.bo.CarPaymentRecord;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("Duplicates")
@Service
public class CarPaymentService {

    @Resource
    private BaseDao BaseDao;

    /**
     * 缴费记录信息
     *
     * @param carPaymentRecord 缴费记录信息
     */
    public void savePaymentRecord(CarPaymentRecord carPaymentRecord) {
        if (carPaymentRecord.getId() == 0) {
            BaseDao.h().save(carPaymentRecord);
        } else {
            BaseDao.h().update(carPaymentRecord);
        }
    }

    /**
     * 查询车辆出入记录
     *
     * @param param Map
     * @return 查询车辆出入列表
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> queryCarPaymentRecordList(Map param) {
        boolean isNotPage = MapUtils.getBooleanValue(param, "isNotPage");
        String sql = "SELECT * " + " FROM car_payment_record  WHERE 1=1 ";
        String parkId = MapUtils.getString(param, "parkId", "");
        if (StringUtils.isNotEmpty(parkId)) {
            sql += " and park_id = '" + parkId + "' ";
        }
        String plateNumber = MapUtils.getString(param, "plateNumber", "");
        if (StringUtils.isNotEmpty(plateNumber)) {
            sql += " and plate_number LIKE '%" + plateNumber + "%' ";
        }
        sql += " order by payment_time desc";
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list;
        if (isNotPage) {
            list = BaseDao.j().queryForList(sql);
        } else {
            Page p = BaseDao.queryPage(sql, Map.class);
            list = p.getList();
            result.put("size", p.getTotalNumber());
        }
        for (Map<String, Object> map : list) {
            int carType = MapUtils.getIntValue(map, "car_type");
            map.put("car_type", CarConstant.carType(carType));
            int paymentType = MapUtils.getIntValue(map, "payment_type");
            map.put("payment_type", CarConstant.carPaymentType(paymentType));
            int paymentMethod = MapUtils.getIntValue(map, "payment_method");
            map.put("payment_method", CarConstant.carPaymentMethod(paymentMethod));
        }
        result.put("list", list);
        result.put("ret_code", 1);
        return result;
    }
}
